package com.lgd.factoryattendance.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lgd.factoryattendance.Bean.ElectricSpinnerBean;
import com.lgd.factoryattendance.R;

import java.util.List;

/**
 * @Author lgd
 * @Date 2024/4/8 15:04
 */
public class ElectricSpinnerAdapter extends BaseAdapter {

    private List<ElectricSpinnerBean> electricSpinnerBeanList;
    private Context mContext;
    private LayoutInflater layoutInflater;

    public ElectricSpinnerAdapter(List<ElectricSpinnerBean> electricSpinnerBeanList, Context mContext) {
        this.electricSpinnerBeanList = electricSpinnerBeanList;
        this.mContext = mContext;
        this.layoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return electricSpinnerBeanList == null ? 0 : electricSpinnerBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return electricSpinnerBeanList == null ? null : electricSpinnerBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = layoutInflater.inflate(R.layout.electric_spinner_custom, parent, false);
            holder = new ViewHolder();
            holder.tv_electricName = convertView.findViewById(R.id.tv_electricName);
            convertView.setTag(holder);
        }else {
            // 如果有可重用的视图，则获取其ViewHolder
            holder = (ViewHolder) convertView.getTag();
        }
        ElectricSpinnerBean electricSpinnerBean = electricSpinnerBeanList.get(position);
        holder.tv_electricName.setText(electricSpinnerBean.getElectricName());
        return convertView;
    }
    static class ViewHolder {
        TextView tv_electricName;
    }


}
